package com.example.xjtuse_pc.mobilesafe;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xjtuse_pc.utils.Md5;

import org.w3c.dom.Text;

import java.security.NoSuchAlgorithmException;

public class HomeActivity extends Activity {
    private static int[] ids = {
            R.drawable.safe, R.drawable.callmsgsafe, R.drawable.app,
            R.drawable.taskmanager, R.drawable.netmanager, R.drawable.trojan,
            R.drawable.sysoptimize, R.drawable.atools, R.drawable.settings

    };
    private Button ok;
    private Button cancel;
    private AlertDialog dialog;
    private EditText et_setup_pwd;
    private EditText et_setup_confirm;
    private GridView list_home;
    private Myadapter adapter;
    private SharedPreferences sp;
    private Md5 md5;
    private String names[] = {
            "手机防盗", "通讯卫士", "软件管理",
            "进程管理", "流量统计", "手机杀毒",
            "缓存清理", "高级工具", "设置中心"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        sp = getSharedPreferences("config", MODE_PRIVATE);
        adapter = new Myadapter();
        md5 = new Md5();
        list_home = (GridView) findViewById(R.id.list_home);
        list_home.setAdapter(adapter);
        list_home.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent;
                switch (position) {
                    case 0://手机防盗
                        showLostFindDialog();
                        break;
                    case 1://通讯卫士
                        intent = new Intent(HomeActivity.this, BlackNumberActivity.class);
                        startActivity(intent);
                        break;
                    case 2://软件管理
                        intent = new Intent(HomeActivity.this, AppManagerActivity.class);
                        startActivity(intent);
                        break;
                    case 3://进程管理
                        intent = new Intent(HomeActivity.this, TaskManageActivity.class);
                        startActivity(intent);
                        break;
                    case 6://缓存清理
                        intent = new Intent(HomeActivity.this, RockerActivity.class);
                        startActivity(intent);
                        break;
                    case 7://高级工具
                        intent = new Intent(HomeActivity.this, AtoolsActivity.class);
                        startActivity(intent);
                        break;

                    case 8://设置中心
                        intent = new Intent(HomeActivity.this, SettingActivity.class);
                        startActivity(intent);
                        break;

                }

            }
        });
    }

    private void showLostFindDialog() {
        //判断是否设置过密码
        if (isSetupPwd()) {
            //已经设置密码了，弹出的是输入对话框
            showEnterDialog();

        } else {
            //输入密码
            showSetupPwdDialog();
        }
    }

    private void showEnterDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
        View view = View.inflate(HomeActivity.this, R.layout.dialog_enter_password, null);
        et_setup_pwd = (EditText) view.findViewById(R.id.et_setup_pwd);
        ok = (Button) view.findViewById(R.id.ok);
        cancel = (Button) view.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = et_setup_pwd.getText().toString().trim();
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(HomeActivity.this, "密码为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                if ((md5.pwd(password)).equals(sp.getString("password", ""))) {
                    Intent intent = new Intent(HomeActivity.this, LastFindActivity.class);
                    startActivity(intent);
                    Toast.makeText(HomeActivity.this, "进入手机防盗", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                } else {
                    Toast.makeText(HomeActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                    et_setup_pwd.setText("");
                    return;
                }
            }
        });
        dialog = builder.create();
        dialog.setView(view);
        dialog.show();

    }

    private void showSetupPwdDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
        View view = View.inflate(HomeActivity.this, R.layout.dialog_setup_password, null);
        et_setup_pwd = (EditText) view.findViewById(R.id.et_setup_pwd);
        et_setup_confirm = (EditText) view.findViewById(R.id.et_setup_confirm);
        ok = (Button) view.findViewById(R.id.ok);
        cancel = (Button) view.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = et_setup_pwd.getText().toString().trim();
                String password_confirm = et_setup_confirm.getText().toString().trim();
                if (TextUtils.isEmpty(password) || TextUtils.isEmpty(password_confirm)) {
                    Toast.makeText(HomeActivity.this, "密码为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (password.equals(password_confirm)) {
                    SharedPreferences.Editor editor = sp.edit();

                    editor.putString("password", md5.pwd(password));
                    editor.commit();
                    dialog.dismiss();

                } else {
                    Toast.makeText(HomeActivity.this, "密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        });
        dialog = builder.create();
        dialog.setView(view);
        dialog.show();

    }

    private boolean isSetupPwd() {
        String password = sp.getString("password", null);
        return !TextUtils.isEmpty(password);
    }


    private class Myadapter extends BaseAdapter {
        @Override
        public int getCount() {
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(HomeActivity.this, R.layout.list_item_home, null);
            ImageView iv_item = (ImageView) view.findViewById(R.id.iv_item);
            TextView tv_item = (TextView) view.findViewById(R.id.tv_item);
            iv_item.setImageResource(ids[position]);
            tv_item.setText(names[position]);

            return view;
        }
    }

}
